package com.ltsw.mvpbaseproject.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.ltsw.mvpbaseproject.R;

/**
 * @author FangHeQiang
 * @description:自定义网络加载框
 * @date :2019/8/28 14:08
 */
public class MyProgressDialog {
    private Dialog dialog;
    private Context context;

    public MyProgressDialog(Context context) {
        this.context = context;
    }


    public void showDialog() {
        showDialog("加载中......");
    }

    public void showDialog(String msg) {
        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.setCancelable(false);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.activity_base_dialog);
        window.setBackgroundDrawable(new BitmapDrawable());//去掉黑边
        TextView textView = (TextView) window.findViewById(R.id.loadingText);
        if (TextUtils.isEmpty(msg)) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(msg);
        }

        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    dialog.dismiss();
                }
                return false;
            }
        });
    }

    public void closeDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
